package com.letv.whatslive.mongo.dao;

import com.google.common.collect.Lists;
import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.Start_Image;
import com.letv.whatslive.model.convert.Start_ImageConvert;
import com.letv.whatslive.mongo.BaseDAO;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by gaoshan on 15-10-23.
 */
@Repository
public class StartImageDAO extends BaseDAO {

    protected String collectionName = "start.image";
    private static final Logger logger = LoggerFactory.getLogger(StartImageDAO.class);
    @Override
    protected void init() {
        super.init(this.collectionName);
    }

    @Override
    protected long getAutoIncrementId() {
        return idGenerate.generateId(this.collectionName);
    }

    public Start_Image getStart_ConstantById(Long id) {
        BasicDBObject query = new BasicDBObject();
        Start_Image start_Image = new Start_Image();
        query.put("_id", id);
        DBObject dbObject = this.dbCollection.findOne(query);
        if (dbObject == null) return null;
        try {
            start_Image = Start_ImageConvert.castDBObjectToStart_Image(dbObject);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return start_Image;
    }

    private BasicDBObject getBasicDBObjectByParams(Map params){
        //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
        BasicDBObject query = new BasicDBObject();// 新建查询基类对象 dbo
        if (params != null && params.size() > 0) {
            if (params.get("_id") != null) {
                query.put("_id", ObjectUtils.toLong(params.get("_id")));
            }
            if (params.get("deviceType") != null) {
                query.put("deviceType", ObjectUtils.toInteger(params.get("deviceType")));
            }
            if (params.get("picSpec") != null) {
                query.put("picSpec", ObjectUtils.toString(params.get("picSpec")));
            }
            if (params.get("constantId") != null) {
                query.put("constantId", ObjectUtils.toLong(params.get("constantId")));
            }
            if (params.get("variableId") != null) {
                query.put("variableId", ObjectUtils.toLong(params.get("variableId")));
            }
        }
        return query;
    }

    public List<Start_Image> getAllAppAllowsByParams(Map<String, Object> params){
        List<Start_Image> result = Lists.newArrayList();
        try {
            //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
            BasicDBObject query = getBasicDBObjectByParams(params);
            DBCursor cur = this.dbCollection.find(query);
            while (cur.hasNext()) {
                DBObject dbObject = cur.next();
                Start_Image start_Image = Start_ImageConvert.castDBObjectToStart_Image(dbObject);
                result.add(start_Image);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result;
    }

    public List<Start_Image> getAppAllowsListByParams(Map<String, Object> params, Integer start, Integer limit) {
        List<Start_Image> result = Lists.newArrayList();
        if (limit == null) {
            limit = DEFAULT_NUM;
        }
        try {
            //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
            BasicDBObject query = getBasicDBObjectByParams(params);
            Integer skip = start == null ? 0 : start;
            DBCursor cur = this.dbCollection.find(query).skip(skip).limit(limit);//按照视频类型和创建时间倒序
            while (cur.hasNext()) {
                DBObject dbObject = cur.next();
                Start_Image start_Image = Start_ImageConvert.castDBObjectToStart_Image(dbObject);
                result.add(start_Image);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Long countProgramByParams(Map params) {
        //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
        BasicDBObject query = getBasicDBObjectByParams(params);
        return this.dbCollection.count(query);
    }

    public Start_Image saveStartImage(Start_Image start_Image) {
        try {
            start_Image.setId(getAutoIncrementId());
            DBObject dbo = Start_ImageConvert.castStart_ImageToDBObject(start_Image);
            WriteResult wr = this.dbCollection.insert(dbo);
            return start_Image;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    public void deleteStartImageByParams(Long id){
        BasicDBObject query = new BasicDBObject();
        query.put("_id", id);
        this.dbCollection.remove(query);
    }
}
